package com.ydsungan.service.impl;

import com.ydsungan.common.DateUtils;
import com.ydsungan.dao.*;
import com.ydsungan.pojo.*;
import com.ydsungan.service.BookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private PracticeMapper practiceMapper;

    @Autowired
    private UsedMapper usedMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VisitorMapper visitorMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addBook(Book book) {
        bookMapper.insert(book);

    }

    @Override
    public List<Book> selectBook(BookExample bookExample) {
        return bookMapper.selectByExample(bookExample);
    }

    @Override
    public Book getBookById(Integer id) {
        return bookMapper.selectByPrimaryKey(id);
    }

    @Override
    public Chapter getChapterById(Integer id) {
        return chapterMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Chapter> listCatalog(ChapterExample chapterExample) {
        return chapterMapper.selectByExample(chapterExample);
    }

    @Override
    public int addLoginRecord(Statistics statistics) {
        return statisticsMapper.insert(statistics);
    }

    @Override
    public long countPracticeByOpenid(PracticeExample practiceExample) {
        return practiceMapper.countByExample(practiceExample);
    }

    @Override
    public List<Statistics> listStatistics(StatisticsExample statisticsExample) {
        return statisticsMapper.selectByExample(statisticsExample);
    }

    @Override
    public List<Used> listUsingBook(UsedExample usedExample) {
        return usedMapper.selectByExample(usedExample);
    }

    @Override
    public List<Book> selectUsingBook(String openId) {

        UsedExample usedExample = new UsedExample();
        UsedExample.Criteria usingCriteria = usedExample.createCriteria();
        usingCriteria.andUserIdEqualTo(openId);
        List<Used> usingList = usedMapper.selectByExample(usedExample);

        List<Book> bookList = new ArrayList<>();

        for(int i = 0; i < usingList.size(); ++i) {
            bookList.add(bookMapper.selectByPrimaryKey(usingList.get(i).getBookId()));
        }
        return bookList;
    }

    @Override
    public List<BookVO> listAllBooks() {

        BookExample bookExample = new BookExample();
        //列出所有书，以及书的使用人数，题目数量
        List<BookVO> bookVOList = new ArrayList<>();
        List<Book> list = bookMapper.selectByExample(bookExample);
        for(int i = 0; i < list.size(); ++i){
            BookVO bookVO = new BookVO();
            BeanUtils.copyProperties(list.get(i), bookVO);
            UsedExample usedExample = new UsedExample();
            UsedExample.Criteria criteria = usedExample.createCriteria();
            criteria.andBookIdEqualTo(list.get(i).getId());
            int hotNum = (int)usedMapper.countByExample(usedExample);
            bookVO.setUsingNum(hotNum);

            QuestionExample questionExample = new QuestionExample();
            QuestionExample.Criteria criteria2 = questionExample.createCriteria();
            criteria2.andBookIdEqualTo(list.get(i).getId());
            int questionNum = (int)questionMapper.countByExample(questionExample);
            bookVO.setQuestionNum(questionNum);
            bookVOList.add(bookVO);
        }
        for(int i = 0; i < bookVOList.size() - 1; ++i){
            for(int j = 0; j < bookVOList.size() - 1; ++j){
                if(bookVOList.get(j + 1).getUsingNum() > bookVOList.get(j).getUsingNum()){
                    BookVO bookVO = bookVOList.get(j);
                    bookVOList.set(j, bookVOList.get(j + 1));
                    bookVOList.set(j + 1, bookVO);
                }
            }
        }
        return bookVOList;
    }

    @Override
    public int getRank(String openId) throws ParseException {
        UserExample userExample = new UserExample();
        List<User> userList = userMapper.selectByExample(userExample);
        Map<String, Integer> map = new HashMap();
        for(int i = 0; i < userList.size(); ++i){
            PracticeExample practiceExample = new PracticeExample();
            PracticeExample.Criteria criteria = practiceExample.createCriteria();
            criteria.andCreateTimeBetween(DateUtils.getDateStartAndEnd(6).get(0), new Date());
            criteria.andUserIdEqualTo(userList.get(i).getId());
            int count = (int)practiceMapper.countByExample(practiceExample);
            map.put(userList.get(i).getId(), count);

        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                // 从大到小排序
                return (o2.getValue() - o1.getValue());
            }
        });
        int rank = 1;
        for(int i = 0; i < list.size(); ++i){
            if(list.get(i).getKey().equals(openId)){
                if(list.get(i).getValue() == 0){
                    rank = -1;
                }else{
                    rank = i + 1;
                }

                break;
            }
        }

        return rank;
    }

    @Override
    public void addVisitorRecord(Visitor visitor) {
        visitorMapper.insert(visitor);
    }

    @Override
    public BookVO getBookDetail(String bookId) {
        BookVO bookVO = new BookVO();

        Book book = bookMapper.selectByPrimaryKey(Integer.parseInt(bookId));
        BeanUtils.copyProperties(book,bookVO);

        QuestionExample questionExample = new QuestionExample();
        QuestionExample.Criteria criteria = questionExample.createCriteria();
        criteria.andBookIdEqualTo(Integer.parseInt(bookId));
        bookVO.setQuestionNum((int)questionMapper.countByExample(questionExample));

        UsedExample usedExample = new UsedExample();
        UsedExample.Criteria criteria1 = usedExample.createCriteria();
        criteria1.andBookIdEqualTo(Integer.parseInt(bookId));
        bookVO.setUsingNum((int)usedMapper.countByExample(usedExample));

        return bookVO;
    }
}
